/********************************************************
*         system calls: use Pipe                        *
*         Parent reads, Child writes                    *
********************************************************/
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <iostream>           /* c++ I/O headers */

int main (int argc, char *argv[]) {
 char buffer[512], bufferParent[ 512];
 int pid, pipeName[2], index, count;

 pipe (pipeName);  /* [0] is 'read' end; [1] is 'write' end */
 pid = fork();

 if (pid == 0) {

        /* child */
        std::cout << "I am the child; my ID" << getpid() << " "<<  std::endl;
        std::cout << "Please enter your first name" << std::endl;

        std::cin >> buffer;       // read from the terminal window
        std::cout << "Child  printing..." 
		<< buffer << std::endl; // print to term window

        close (pipeName[0]);    //I don't read off of pipe
        write (pipeName[1], buffer, strlen(buffer));
        close (pipeName[1]);    //all done with writing
        exit(1);

  }

 else{      /* parent */
        std::cout << "I am the parent; my ID = " << getpid() << std::endl;
        close (pipeName[1]);    //I don't  write to pipe

        count = read (pipeName[0], bufferParent, 512);
        std::cout << "parent printing..." ;
        for (index=0; index < count; index++)
                std::cout << bufferParent[index] ;
        std::cout << std::endl;

        close (pipeName[0]);    //all done with reading



 }
}

/**
bgunix% pwd
/home/cs/kresman/327
bgunix% g++ pipe.cpp
bgunix% a.out
I am the parent; my ID = 22810
I am the child; my ID22811
Please enter your first name
Ray
Child  printing...Ray
parent printing...Ray
*/
